package com.example.myFirstWebmagic.airQualityForecasting;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.myFirstWebmagic.pipeline.ConsolePipelineDemo;
import com.example.myFirstWebmagic.util.DateUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 
 * ClassName: AirqualityForecastProcess 
 * @Description: 空气质量预报爬取
 * @author xiongyao
 * @date 2017年3月9日
 */
public class AirqualityForecastProcess implements PageProcessor{

	 Logger log=LoggerFactory.getLogger(AirqualityForecastProcess.class);
	 
	 private Site site = Site.me().setRetryTimes(10).setSleepTime(5000);
	
	 
	 public static void main(String[] args) {
			// TODO Auto-generated method stub
			String [] startUrl=new String[]{"http://106.37.208.228:8082/Home/Default"};
			// TODO Auto-generated method stub
			Spider.create(new AirqualityForecastProcess())
	        //从"http://datacenter.mep.gov.cn/index!getCityMain.action"开始抓
	        .addUrl(startUrl)
	        .addPipeline(new ConsolePipelineDemo())
	        //开启5个线程抓取
	        .thread(5)
	        //启动爬虫
	        .run();
			
		}
	 
	 
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		ScriptContext context = engine.getContext();
		String jsContext=page.getHtml().xpath("/html/body/script[11]").get().replaceAll("<script>", "").replaceAll("</script>", "");
		
		try {
			engine.eval(jsContext.substring(0,jsContext.indexOf("var map = new MapOperate(),")));
		} catch (ScriptException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Gson gson=new Gson();
		//数据更新时间
		String updateTime=page.getHtml().xpath("/html/body/div[1]/span/text()").get().replaceAll("发布", "");
		//数据更新事件格式转换
		updateTime=DateUtil.format(updateTime, DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时"));
		//存放所有的空气质量预报
		List<EnAirqualityForecast> forecasts=new ArrayList<EnAirqualityForecast>();
		//获取了所有的城市空气质量预报
		JsonObject array=new JsonParser().parse(new Gson().toJson(context.getAttribute("cities"))).getAsJsonObject();
		Set<Entry<String, JsonElement>> entrySet =  array.entrySet();
		for(Entry<String, JsonElement> report : entrySet){
		    JsonObject obj = array.getAsJsonObject(report.getKey());
		    EnAirqualityForecast forecast=gson.fromJson(obj, EnAirqualityForecast.class);
		    AirqualityForecastPK id=new AirqualityForecastPK();
		    id.setDate(updateTime);
		    id.setCity(forecast.getName());
		    forecast.setId(id);
		    forecasts.add(forecast);
		}
		page.putField("objectList", forecasts);
		
	}

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return this.site;
	}

	
}
